Completed
Push — master ( 5aa3de...41f335 )
by Andres
33s
created

angular.service(ꞌutilꞌ)   B

Complexity

Conditions 1
Paths 8

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
c 4
b 0
f 0
nc 8
dl 0
loc 42
rs 8.8571
nop 4

4 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 10 4
A ��) 0 3 1
A ��) 0 7 3
B ��) 0 15 6
1
/* globals numberformat */
2
/**
3
 util
4
 Utility service with misc. functions.
5
6
 @namespace Services
7
 */
8
'use strict';
9
10
angular
11
  .module('game')
12
  .service('util', ['prettyNumberFilter',
13
    '$sce',
14
    'data',
15
    'state',
16
    function(prettyNumber, $sce, data, state) {
17
      /* Return the HTML representation of an element, or the element itself
18
      if it doesn't have one */
19
      this.getHTML = function(resource) {
20
        let html = data.html[resource];
21
        if (typeof html === 'undefined' && data.resources[resource]) {
22
          html = data.resources[resource].html;
23
        }
24
        if (typeof html === 'undefined') {
25
          return resource;
26
        }
27
        return html;
28
      };
29
30
      this.prettifyNumber = function(number) {
31
        if (typeof number === 'undefined' || number === null) {
32
          return null;
33
        }
34
        if (number === '') {
35
          return '';
36
        }
37
        if (number === Infinity) {
38
          return '∞';
39
        }
40
        if (number === 0) {
41
          return '0';
42
        }
43
        return numberformat.format(number, state.player.numberformat);
44
      };
45
46
      this.addResource = function(resource, key, quantity){
47
        resource.number += quantity;
48
        if (quantity > 0 && !resource.unlocked) {
49
          resource.unlocked = true;
50
          state.addNew(key);
51
        }
52
      }
53
54
      this.trustHTML = function(html) {
55
        return $sce.trustAsHtml(html);
56
      };
57
    }
58
  ]);
59